<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapper.packageName}.${entity.name}${mapperSuffix}">

	<!-- =================================对象映射============================== -->
	
	<resultMap id="${entity.name}Map" type="${entity.fullName}">
		<#list table.fields as field>
			<#if field.name == 'id' >
		<id property="id" column="id" jdbcType="${field.jdbcType}"/>
			</#if> 
			<#if field.name != 'id' >
		<result property="${field.javaName}" column="${field.name}" jdbcType="${field.jdbcType}"/>
			</#if> 
		</#list>
	</resultMap>

	<!-- ===================================字段集============================== -->
	
	<sql id="columns">
		<#list table.fields as field>
			<#if field_has_next>		                             
		${field.name},
			<#else>
		${field.name}
			</#if> 
		</#list>
	</sql>
	  
	<!-- ===================================属性集============================= -->
	
	<sql id="insertProperties">
        <#list table.fields as field>
            <#if field.name == 'version'>
                <#if field_has_next>
        0,
                <#else>
        0
                </#if>
            <#else>
                <#if field_has_next>
        <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}},
                <#else>
        <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}}
                </#if>
            </#if>
        </#list>
	</sql>
	
	<sql id="batchProperties">
        <#list table.fields as field>
            <#if field.name == 'version'>
                <#if field_has_next>
        0,
                <#else>
        0
                </#if>
            <#else>
                <#if field_has_next>
        <#noparse>#{</#noparse>item.${field.javaName},jdbcType=${field.jdbcType}},
                <#else>
        <#noparse>#{</#noparse>item.${field.javaName},jdbcType=${field.jdbcType}}
                </#if>
            </#if>
        </#list>
	</sql>
	
	<!-- ==================================条件集================================= -->
	
	<sql id="conditionElement">
		<if test="@${mapper.ognlClass}@isNotEmpty(condition)">
			<#list table.fields as field>
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.${field.javaName})">
				AND ${field.name} = <#noparse>#{</#noparse>condition.${field.javaName},jdbcType=${field.jdbcType}}
			</if>
			<#if field.name == 'create_time' >
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.startCreateTime)">
				<![CDATA[ AND create_time >= <#noparse>#{</#noparse>condition.startCreateTime,jdbcType=VARCHAR} ]]>
			</if>
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.endCreateTime)">
				<![CDATA[ AND create_time < <#noparse>#{</#noparse>condition.endCreateTime,jdbcType=VARCHAR} ]]>
			</if>
			</#if>
			<#if field.name == 'update_time' >
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.startUpdateTime)">
				<![CDATA[ AND update_time >= <#noparse>#{</#noparse>condition.startUpdateTime,jdbcType=VARCHAR} ]]>
			</if>
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.endUpdateTime)">
				<![CDATA[ AND update_time < <#noparse>#{</#noparse>condition.endUpdateTime,jdbcType=VARCHAR} ]]>
			</if>
			</#if>
			<#if field.name == 'last_time' >
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.startLastTime)">
				<![CDATA[ AND update_time >= <#noparse>#{</#noparse>condition.startLastTime,jdbcType=VARCHAR} ]]>
			</if>
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.endLastTime)">
				<![CDATA[ AND last_time < <#noparse>#{</#noparse>condition.endLastTime,jdbcType=VARCHAR} ]]>
			</if>
			</#if>
			</#list>
			<if test="@${mapper.ognlClass}@isNotEmpty(condition.ids)">
				<#list table.fields as field>
				<#if field.name == 'id' >
				AND id IN
				<foreach collection="condition.ids" item="item" index="index" separator="," open="(" close=")">
	  				<#noparse>#{</#noparse>item,jdbcType=${field.jdbcType}} 
	  			</foreach>
				</#if> 
				</#list>
			</if>
		</if>
	</sql>
 
	<sql id="conditionWhere">
        <#list table.fields as field>
        <if test="@${mapper.ognlClass}@isNotEmpty(${field.javaName})">
            AND ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}}
        </if>
            <#if field.name == 'date' >
        <if test="@${mapper.ognlClass}@isNotEmpty(startTime)">
            <![CDATA[ AND date >= <#noparse>#{</#noparse>startTime,jdbcType=VARCHAR} ]]>
        </if>
        <if test="@${mapper.ognlClass}@isNotEmpty(endTime)">
            <![CDATA[ AND date < <#noparse>#{</#noparse>endTime,jdbcType=VARCHAR} ]]>
        </if>
            </#if>
            <#if field.name == 'create_time' >
        <if test="@${mapper.ognlClass}@isNotEmpty(startCreateTime)">
            <![CDATA[ AND create_time >= <#noparse>#{</#noparse>startCreateTime,jdbcType=VARCHAR} ]]>
        </if>
        <if test="@${mapper.ognlClass}@isNotEmpty(endCreateTime)">
            <![CDATA[ AND create_time < <#noparse>#{</#noparse>endCreateTime,jdbcType=VARCHAR} ]]>
        </if>
            </#if>
            <#if field.name == 'update_time' >
        <if test="@${mapper.ognlClass}@isNotEmpty(startUpdateTime)">
            <![CDATA[ AND update_time >= <#noparse>#{</#noparse>startUpdateTime,jdbcType=VARCHAR} ]]>
        </if>
        <if test="@${mapper.ognlClass}@isNotEmpty(endUpdateTime)">
            <![CDATA[ AND update_time < <#noparse>#{</#noparse>endUpdateTime,jdbcType=VARCHAR} ]]>
        </if>
            </#if>
            <#if field.name == 'last_time' >
        <if test="@${mapper.ognlClass}@isNotEmpty(startLastTime)">
            <![CDATA[ AND update_time >= <#noparse>#{</#noparse>startLastTime,jdbcType=VARCHAR} ]]>
        </if>
        <if test="@${mapper.ognlClass}@isNotEmpty(endLastTime)">
            <![CDATA[ AND last_time < <#noparse>#{</#noparse>endLastTime,jdbcType=VARCHAR} ]]>
        </if>
            </#if>
        </#list>
        <if test="@${mapper.ognlClass}@isNotEmpty(ids)">
            <#list table.fields as field>
                <#if field.name == 'id' >
            AND id IN
            <foreach collection="ids" item="item" index="index" separator="," open="(" close=")">
                <#noparse>#{</#noparse>item,jdbcType=${field.jdbcType}}
            </foreach>
                </#if>
            </#list>
        </if>

        <#if entity.hasName || entity.hasTitle || entity.idAttr.type == 'String'>
        <!-- 模糊查询 -->
        </#if>
        <#if entity.idAttr.type == 'String' >
        <if test="@cn.demoncat.util.mybatis.util.MapperOgnl@isNotEmpty(idLike)">
            AND id LIKE CONCAT(<#noparse>#{</#noparse>idLike,jdbcType=VARCHAR}, '%')
        </if>
        </#if>
        <#if entity.hasName >
        <if test="@cn.demoncat.util.mybatis.util.MapperOgnl@isNotEmpty(nameLike)">
            AND name LIKE CONCAT('%', <#noparse>#{</#noparse>nameLike,jdbcType=VARCHAR}, '%')
        </if>
        </#if>
        <#if entity.hasTitle >
        <if test="@cn.demoncat.util.mybatis.util.MapperOgnl@isNotEmpty(titleLike)">
            AND title LIKE CONCAT('%', <#noparse>#{</#noparse>titleLike,jdbcType=VARCHAR}, '%')
        </if>
        </#if>
	</sql>
	 
	<!-- ===================================更新集================================= -->
	
	<sql id="updateSet">
        <#list table.fields as field>
            <#if field.name != 'id'>
                <#if field.name == 'version'>
                    <#if field_has_next>
        version = version+1,
                    <#else>
        version = version+1
                    </#if>
                <#else>
                    <#if field_has_next>
    <if test="${field.javaName} != null">
        ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}},
    </if>
                    <#else>
    <if test="${field.javaName} != null">
        ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}}
    </if>
                    </#if>
                </#if>
            </#if>
        </#list>
	</sql>
	
	<sql id="fullUpdateSet">
        <#list table.fields as field>
            <#if field.name != 'id'>
                <#if field.name == 'version'>
                    <#if field_has_next>
        version = version+1,
                    <#else>
        version = version+1
                    </#if>
                <#else>
                    <#if field_has_next>
        ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}},
                    <#else>
        ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}}
                    </#if>
                </#if>
            </#if>
        </#list>
	</sql>
	
	<sql id="updateObjSet">
        <#list table.fields as field>
            <#if field.name != 'id'>
                <#if field.name == 'version'>
                    <#if field_has_next>
        version = version+1,
                    <#else>
        version = version+1
                    </#if>
                <#else>
                    <#if field_has_next>
    <if test="obj.${field.javaName} != null">
        ${field.name} = <#noparse>#{</#noparse>obj.${field.javaName},jdbcType=${field.jdbcType}},
    </if>
                    <#else>
    <if test="obj.${field.javaName} != null">
        ${field.name} = <#noparse>#{</#noparse>obj.${field.javaName},jdbcType=${field.jdbcType}}
    </if>
                    </#if>
                </#if>
            </#if>
        </#list>
	</sql>
	
	<sql id="fullUpdateObjSet">
        <#list table.fields as field>
            <#if field.name != 'id'>
                <#if field.name == 'version'>
                    <#if field_has_next>
        version = version+1,
                    <#else>
        version = version+1
                    </#if>
                <#else>
                    <#if field_has_next>
        ${field.name} = <#noparse>#{</#noparse>obj.${field.javaName},jdbcType=${field.jdbcType}},
                    <#else>
        ${field.name} = <#noparse>#{</#noparse>obj.${field.javaName},jdbcType=${field.jdbcType}}
                    </#if>
                </#if>
            </#if>
        </#list>
	</sql>
	
	<!-- ============================插入==================================== -->
	
	<insert id="save" useGeneratedKeys="true" keyProperty="id" parameterType="${entity.fullName}">
        INSERT INTO ${table.name}(
        	<include refid="columns"/>
        ) VALUES (
			<include refid="insertProperties"/>
        )
	</insert>
	
	<!-- ============================批量插入================================== -->
	
    <insert id="batchSave" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
		INSERT INTO ${table.name}(
			<include refid="columns"/>
		) VALUES 
  			<foreach collection="list" item="item" index="index" separator=",">
  				(<include refid="batchProperties"/>)
  			</foreach>
	</insert>
	
	<!-- ============================删除==================================== -->	
		
    <delete id="delete" parameterType="java.util.Map">
        DELETE FROM ${table.name} 
    	<where>
    		<include refid="conditionWhere"/>
    	</where>
    </delete>
     
    <!-- ==========================更新(排除null)============================= -->
    
	<update id="update"  parameterType="${entity.fullName}">
        UPDATE ${table.name} 
        <set>
			<include refid="updateSet"/>
		</set>
	        <#list table.fields as field>
			<#if field.name == 'id' >
		WHERE id = <#noparse>#{</#noparse>id,jdbcType=${field.jdbcType}} 
			</#if> 
			<#if field.name == 'version'>
			<if test="@${mapper.ognlClass}@isNotEmpty(version)">
				 AND version = <#noparse>#{</#noparse>version,jdbcType=${field.jdbcType}}
			</if>
			</#if>
			</#list> 
	</update>
	
	<!-- ==========================更新(不排除null)============================= -->
    
	<update id="fullUpdate"  parameterType="${entity.fullName}">
        UPDATE ${table.name} 
        <set>
			<include refid="fullUpdateSet"/>
		</set>
	        <#list table.fields as field>
			<#if field.name == 'id' >
		WHERE id = <#noparse>#{</#noparse>id,jdbcType=${field.jdbcType}} 
			</#if> 
			<#if field.name == 'version'>
			<if test="@${mapper.ognlClass}@isNotEmpty(version)">
				 AND version = <#noparse>#{</#noparse>version,jdbcType=${field.jdbcType}}
			</if>
			</#if>
			</#list> 
	</update>
	
	<!-- ==========================条件更新(排除null)========================== -->
	 
	<update id="updateByCondition" parameterType="java.util.Map">
    	UPDATE ${table.name} 
    	<set>  
    		<include refid="updateObjSet"/>
    	</set> 
		<where>
			<if test="@${mapper.ognlClass}@isNotEmpty(obj.id)">
				id = <#noparse>#{</#noparse>obj.id,jdbcType=${table.idField.jdbcType}}
			</if>
			<include refid="conditionElement"/>
    	</where>  
	</update>
	
	<!-- ==========================条件更新(不排除null)========================== -->
	 
	<update id="fullUpdateByCondition" parameterType="java.util.Map">
    	UPDATE ${table.name} 
    	<set>  
    		<include refid="fullUpdateObjSet"/>
    	</set> 
		<where>
			<if test="@${mapper.ognlClass}@isNotEmpty(obj.id)">
				id = <#noparse>#{</#noparse>obj.id,jdbcType=${table.idField.jdbcType}}
			</if>
			<include refid="conditionElement"/>
    	</where>  
	</update>
    	
	<!-- ==========================批量更新(排除null)========================== -->
	 
	<update id="batchUpdate"  parameterType="java.util.List">
    	<foreach collection="list" item="obj" index="index" open="" close="" separator=";">  
        	UPDATE ${table.name} 
        	<set>  
        		<include refid="updateObjSet"/>
        	</set>
			WHERE id = <#noparse>#{</#noparse>obj.id,jdbcType=${table.idField.jdbcType}}
        </foreach>  
	</update>
	
	<!-- ==========================批量更新(不排除null)========================== -->
	 
	<update id="batchFullUpdate"  parameterType="java.util.List">
    	<foreach collection="list" item="obj" index="index" open="" close="" separator=";">  
        	UPDATE ${table.name} 
        	<set>  
        		<include refid="fullUpdateObjSet"/>
        	</set>
			WHERE id = <#noparse>#{</#noparse>obj.id,jdbcType=${table.idField.jdbcType}}
        </foreach>  
	</update>
	
	<!-- ==========================批量条件更新(排除null)====================== -->
	 
	<update id="batchUpdateByCondition"  parameterType="java.util.Map">
    	<foreach collection="objs" item="obj" index="index" open="" close="" separator=";">  
        	UPDATE ${table.name} 
        	<set>  
        		 <include refid="updateObjSet"/>
        	</set>
			WHERE id = <#noparse>#{</#noparse>obj.id,jdbcType=${table.idField.jdbcType}}
       		<include refid="conditionElement"/>
        </foreach>  
	</update>
	 
	<!-- ==========================批量条件更新(不排除null)====================== -->
	 
	<update id="batchFullUpdateByCondition" parameterType="java.util.Map">
    	<foreach collection="objs" item="obj" index="index" open="" close="" separator=";">  
        	UPDATE ${table.name} 
        	<set>  
        		 <include refid="fullUpdateObjSet"/>
        	</set>
			WHERE id = <#noparse>#{</#noparse>obj.id,jdbcType=${table.idField.jdbcType}}
       		<include refid="conditionElement"/>
        </foreach>  
	</update>
	
	<!-- ==========================统一字段更新(排除null)====================== -->
	
	<update id="updateFields" parameterType="java.util.Map">
    	UPDATE ${table.name} 
    	<set>  
        	<include refid="updateObjSet"/>
    	</set>  
    	<where>
       		<include refid="conditionElement"/>
       	</where>
	</update>
	 
    <!-- ============================ID查询=============================== -->
    
    <select id="getById" resultMap="${entity.name}Map">
		SELECT <include refid="columns" />
		FROM ${table.name}
		WHERE id = <#noparse>#{</#noparse>id,jdbcType=${table.idField.jdbcType}}
	</select>
	
	<!-- =========================ID查询(悲观锁)============================ -->
    
    <select id="getForUpdate" resultMap="${entity.name}Map">
		SELECT <include refid="columns" />
		FROM ${table.name}
		WHERE id = <#noparse>#{</#noparse>id,jdbcType=${table.idField.jdbcType}}
		FOR UPDATE
	</select>
	
	<!-- ============================查询ID列表============================= -->
	
	<select id="findIds" resultType="java.lang.${entity.idAttr.type}">
    	SELECT id
	    FROM ${table.name}
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
		<if test="@${mapper.ognlClass}@isNotEmpty(sortColumns)">
			ORDER BY ${r'${sortColumns}'}
		</if>
		<if test="@${mapper.ognlClass}@isNotEmpty(limitNum)">
            LIMIT ${r'${limitNum}'}
        </if>
    </select>
	
	<!-- ============================查询记录数============================= -->
	
    <select id="countByCondition" resultType="long">
        SELECT count(*) FROM ${table.name} 
        <where>
       		<include refid="conditionWhere"/>
       	</where>
    </select>
 
    <!-- ============================分页排序查询============================ -->

    <select id="findByCondition" resultMap="${entity.name}Map">
    	SELECT <include refid="columns" />
	    FROM ${table.name} 
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
		<if test="@${mapper.ognlClass}@isNotEmpty(sortColumns)">
			ORDER BY ${r'${sortColumns}'}
		</if>
		<if test="@${mapper.ognlClass}@isNotEmpty(limitNum)">
            LIMIT ${r'${limitNum}'}
        </if>
    </select>
	 
	<#if entity.baseType == "Report"> 
		<#list entity.attrs as attr>  
			<#if attr.name == "count">
			
	<!-- ====================Report：统计总数 SUM(count)==================== -->
	
	<select id="countCount" resultType="long">
        SELECT IFNULL(SUM(count),0)
        FROM ${table.name} 
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
    </select>		
			</#if>
		</#list>
		<#list entity.attrs as attr>  
			<#if attr.name == "sum">
			
	<!-- =====================Report：统计总额 SUM(sum)===================== -->
	
	<select id="countSum" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(sum),0)
        FROM ${table.name} 
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
    </select>				
			</#if>
		</#list>
		<#list entity.attrs as attr>  
			<#if attr.name == "count">
			
	<!-- ====================Report：统计图表 SUM(count)==================== -->
	
	<select id="chartCount" resultMap="${entity.name}Map">
    	SELECT ${r'${groupColumns}'},SUM(count) AS count
	    FROM ${table.name} 
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
       		GROUP BY ${r'${groupColumns}'}
       	<if test="@${mapper.ognlClass}@isNotEmpty(sortColumns)">
			ORDER BY ${r'${sortColumns}'}
		</if>
    </select>		
			</#if>
		</#list>
		<#list entity.attrs as attr>  
			<#if attr.name == "sum">
			
	<!-- =====================Report：统计图表 SUM(sum)===================== -->
	
	<select id="chartSum" resultMap="${entity.name}Map">
    	SELECT ${r'${groupColumns}'},SUM(sum) AS sum
	    FROM ${table.name} 
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
       		GROUP BY ${r'${groupColumns}'}
       	<if test="@${mapper.ognlClass}@isNotEmpty(sortColumns)">
			ORDER BY ${r'${sortColumns}'}
		</if>
    </select>	
			</#if>
		</#list>
		<#list entity.attrs as attr>  
			<#if attr.name == "count">
				<#list entity.attrs as attr>  
					<#if attr.name == "sum">
					
	<!-- ==================Report：统计图表 SUM(count,sum)================== -->
	
	<select id="charts" resultMap="${entity.name}Map">
    	SELECT ${r'${groupColumns}'},SUM(count) AS count,SUM(sum) AS sum
	    FROM ${table.name} 
	    <where>
       		<include refid="conditionWhere"/>
       	</where>
       		GROUP BY ${r'${groupColumns}'}
       	<if test="@${mapper.ognlClass}@isNotEmpty(sortColumns)">
			ORDER BY ${r'${sortColumns}'}
		</if>
    </select>						
					</#if>
				</#list>
			</#if>
		</#list>
	</#if>
	 
	<!-- ============================== 更新日志 ========================= -->
    <!-- 
    
     -->
     
</mapper>